<!-- 发票列表 -->
<template>
    <div >
        <div class='table-page-search-wrapper'>
            <a-form layout='inline'>
               <a-row :gutter='48'>
                    <a-col :md='8' :sm='24'>
                        <a-form-item label="供应商">
                            <a-input v-model="companyName" placeholder="请输入供应商名称" allow-clear></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                        <a-form-item label="开票内容">
                            <a-input v-model="receiptName" placeholder="请输入开票内容" allow-clear></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                        <a-form-item label="发票类型">
                            <a-select v-model="receiptType" placeholder="请选择" allow-clear>
                                <a-select-option v-for="item in receiptTypeList" :key="item.id" :value="item.id">{{item.name}}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                        <a-form-item label="状态">
                            <a-select v-model="state" placeholder="请选择" allow-clear>
                                <a-select-option v-for="item in stateList" :key="item.id" :value="item.id">{{item.name}}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                        <a-form-item label="申请时间">
                            <a-range-picker @change="onDateChange" allow-clear/>
                        </a-form-item>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                        <a-form-item >
                            <a-button type='primary' @click="getData">查询</a-button>
                            <a-button style="margin-left: 20px" type='primary' @click="add">申请发票</a-button>
                        </a-form-item>
                    </a-col>
                </a-row>
            </a-form>
        </div>  

        <a-table rowKey="id" :loading="isload" :columns="columns" :data-source="dataList" @change="handleTableChange" :pagination="{ total: total ,showSizeChanger: true,pageSize: size,}"  size="small" :scroll="{ x: 1500, }">
            <div slot="receiptType" slot-scope="receiptType">
                <div v-for="(item,i) in receiptTypeList" :key="item.id">
                    <a-tag v-if="item.id == receiptType" :color="colors[i%13]">{{item.name}}</a-tag>
                </div>
            </div>
            <div slot="state" slot-scope="state">
                <div v-for="(item,i) in stateList" :key="item.id">
                    <a-tag v-if="item.id == state" :color="colors[i%13]">{{item.name}}</a-tag>
                </div>
            </div>
            <div slot="receiptName" slot-scope="receiptName">
                <ellipsis :length="12" tooltip>{{ receiptName == '' || !receiptName ? '-' : receiptName }}</ellipsis>
            </div>
            <div slot="remark" slot-scope="remark">
                <ellipsis :length="12" tooltip>{{ remark == '' || !remark ? '-' : remark }}</ellipsis>
            </div>
            <div slot="operation" slot-scope="text,record">
                <div class="editable-row-operations">
                    <a-button v-if="record.state == 1" type="link" @click="cancel(record)">撤销</a-button>
                    <a-button type="link" @click="detail(record)">详情</a-button>
                </div>
            </div>
        </a-table>
    </div>
</template>

<script>
import { columns } from './table';
import { getReceiptList } from '@/api/services/invoice';
import { cancelReceipt } from '@/api/companys/invoice';
import { Ellipsis } from '@/components';
export default {
    components: {
        Ellipsis,
    },
    data () {
        return {
            dataList: [],
            isload: false,
            page: 1,
            size: 10,
            total: 0,
            columns,
            startTime: '',
            endTime: '',
            companyName: '', // 供应商名称
            receiptName: '', // 开票内容
            receiptType: undefined, // 发票类型
            receiptTypeList: [
                { id: 1, name: '增值税普通发票'},
                { id: 2, name: '增值税专用发票'},
            ],
            state: undefined,
            stateList: [
                { id: 1, name: '待审核'},
                { id: 2, name: '待开票'}, 
                { id: 3, name: '待邮寄'}, 
                { id: 4, name: '已邮寄'}, 
                { id: 8, name: '已拒绝'}, 
                { id: 9, name: '已取消'}, 
            ],
            refuseVisible: false,
            refuseReason: '',
            makeVisible: false,
            invoiceCode: '', // 发票查询码
            invoiceFile: {},
            invoiceFileList: [],
            mailVisible: false, 
            invoice: {
                id: ''
            },
        };
    },
    created(){
        this.getData();
    },
    methods: {
        // 日期筛选
        onDateChange(date,dateStr){
            console.log(date,dateStr)
            if(date.length > 0){
                this.startTime = `${dateStr[0]}`
                this.endTime = `${dateStr[1]}`
            }else{
                this.startTime = ''
                this.endTime = ''
            }
            this.getData()
        },
        // 获取账户列表数据
        getData(){
            let _this = this;
            _this.isload = true;
            const params = {
                'startTime': _this.startTime,
                'endTime': _this.endTime,
                'companyName': _this.companyName, 
                'receiptName': _this.receiptName,
                'receiptType': _this.receiptType,
                'state': _this.state,
                'pageSize': _this.size,
                'pageIndex': _this.page,
            }
            getReceiptList(params)
            .then(res => {
                _this.isload = false;
                if(res.success){
                    _this.dataList = res.data;
                    _this.total = res.totalCount
                }else{
                    _this.$notification['error']({
                        message: '温馨提示',
                        description: res.errMessage,
                        duration: 8
                    })
                }
            })
        },
        // 分页
        handleTableChange(pagination, filters, sorter) {
            console.log(pagination);
            this.page = pagination.current;
            this.size = pagination.pageSize;
            this.getData()
        },
        // 申请发票
        add(){
            this.$router.push({ path: '/companysInvoice/invoiceApply'});
        },
        // 详情
        detail(item){
            this.$router.push({ path: '/companysInvoice/invoiceDetail', query: { 'receiptId': item.id }});
        },
        // 撤销发票申请
        cancel(item){
            let _this = this;
            _this.$confirm({
                title: '温馨提示',
                content: '撤销后，供应商将不再处理该发票申请，请谨慎操作；',
                onOk() {
                    cancelReceipt(item.id)
                    .then(res => {
                        if(res.success){
                            _this.$notification['success']({
                                message: '温馨提示',
                                description: '操作成功',
                                duration: 8
                            })
                            _this.getData();
                        }else{
                            _this.$notification['error']({
                                message: '温馨提示',
                                description: res.errMessage,
                                duration: 8
                            })
                        }
                    })
                },
                onCancel() {},
            });
        },
    }
}

</script>
<style scoped>

</style>